package com.century.sjt.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.activity.Setting_pwdPayActivity;
import com.century.sjt.util.Base64Utils;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;
import com.century.sjt.widget.gridpasswordview.GridPasswordView;

import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:周玲波
 * @Description: 再次确认密码
 * @date : 2016/8/19 17:21
 */
public class Setting_AnewpwdPayFragment extends Fragment implements Setting_pwdPayActivity.AnewClickListener {
    private String oldPwd;
    private String sn;
    private String newPwd;
    private GridPasswordView new_payPwd2;
    //private Button btn_Submit;
    private RequestQueue mQueue;
    public static SharedPreferences mainSharedPreferences;
    private String isCashPwdSet;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sjt_modify_anewpwd_fragment, null);
        new_payPwd2 = (GridPasswordView) view.findViewById(R.id.paypwd_new2);
        // btn_Submit=(Button)view.findViewById(R.id.btn_submit_paypwd);
        mQueue = Volley.newRequestQueue(getActivity());
        mainSharedPreferences = getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
        isCashPwdSet = mainSharedPreferences.getString("isCashPwdSet", "");

        initView();
        return view;

    }

    @Override
    public void onANewClick(String oldPwd, String newPwd,String sn) {
        this.oldPwd = oldPwd;
        this.newPwd = newPwd;
        this.sn=sn;
    }

    @Override
    public void onANewClick(String newPwd) {
        this.newPwd = newPwd;
    }

    private void initView() {
        new_payPwd2.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onChanged(String psw) {

            }

            @Override
            public void onMaxLength(String psw) {
                if (!(new_payPwd2.getPassWord().length() == 6)) {
                    TipUtil.showToast(getResources().getString(R.string.error_again_no6_password), getActivity(), 1);
                } else {
                    byte[] encodePwd = new_payPwd2.getPassWord().getBytes();
                    String sureNewPwd= Base64Utils.encode(encodePwd);
                    if (sureNewPwd.equals(newPwd)){
                        if (!(isCashPwdSet.equals("1"))) {
                            setPassWard();//设置密码
                        } else {
                            if (oldPwd.equals(sureNewPwd)){
                                TipUtil.showToast(getResources().getString(R.string.error_pwd_likeliness), getActivity(), 1);
                            }else {
                                queryPassWard();//修改密码
                            }
                        }
                    }else {
                        TipUtil.showToast(getResources().getString(R.string.error_pwd_unlikeliness), getActivity(), 1);
                    }
                }
            }
        });



    }
    //修改支付密码

    private void queryPassWard() {
        //显示Progress Dialog
        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), getActivity());
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.SetPayPassWord, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    TipUtil.closeProgressDialog();
                    TipUtil.log("获取成功", response.toString(), 1);
                    JSONTokener jsonParser = new JSONTokener(response);
                    JSONObject res = (JSONObject) jsonParser.nextValue();
                    String code = res.getString("code");
                    Log.d("code", code);
                    String message = res.getString("message");
                    if (code.equals("200")) {//成功
                        SharedPreferences.Editor editor = mainSharedPreferences.edit();
                        editor.putString("isCashPwdSet", "1");
                        editor.commit();
                        TipUtil.showToast(getResources().getString(R.string.text_update_success), getActivity(), 1);
                        getActivity().finish();
                    } else {//失败
                        TipUtil.showToast(message, getActivity(), 1);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    TipUtil.showToast(getResources().getString(R.string.error_service), getActivity(), 1);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), getActivity(), 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");
                map.put("loginId", userId);
                map.put("password", newPwd.trim());
                map.put("sn",sn);
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);
    }

    //设置支付密码
    private void setPassWard() {
        //显示Progress Dialog
        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), getActivity());
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.NewPayPassWord, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    TipUtil.closeProgressDialog();

                    JSONTokener jsonParser = new JSONTokener(response);
                    JSONObject res = (JSONObject) jsonParser.nextValue();
                    String code = res.getString("code");
                    Log.d("code", code);
                    String message = res.getString("message");
                    if (code.equals("200")) {//成功
                        SharedPreferences.Editor editor = mainSharedPreferences.edit();
                        editor.putString("isCashPwdSet", "1");
                        editor.commit();
                        TipUtil.showToast(getResources().getString(R.string.error_set_success), getActivity(), 1);
                        getActivity().finish();
                    } else {//失败
                        TipUtil.showToast(message, getActivity(), 1);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    TipUtil.showToast(getResources().getString(R.string.error_service), getActivity(), 1);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), getActivity(), 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");
                int i;
                map.put("loginId", userId);
                map.put("password", newPwd.trim());
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);
    }
}
